$(function () {

    var init=function (params) {

        var postUrl = '/commodity/doInsert';

        var OptionsTpl =function (arr) {
            var Options  = ""
            for(var i=0; i < arr.length; i++){
                Options += '<option  value="' + arr[i].id + '" >' + arr[i].title + '</option>';
            }


            return Options
        }

        var editor = $.createEditor({
            id:"#editor"
        });

        var extra = $.createEditor({
            id:"#extra"
        });

        //上传 部分
        var CoverUrl = "";

        var uploaderCoverUrl = function ($updom,url) {
            CoverUrl = url;
            $updom.find('.upresult').attr("src", url);
        };


        $.initWebuploader({
            updom: $("#uploader-cover"),
            label: "上传",
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            },
            success: function (res,$updom) {
                uploaderCoverUrl($updom,res.data)
            }
        })

        var $form =  $('form').bootstrapValidator({
            fields:{
                title: {
                    validators: {
                        notEmpty: {
                            message: '产品名称不能为空'
                        }
                    }
                },
                currentCost: {
                    validators: {
                        notEmpty: {
                            message: '现价不能为空'
                        },
                        regexp: {
                            regexp: /^[0-9\.]+$/,
                            message: '请填写正确的价格'
                        }
                    }
                },
                backIndent: {
                    validators: {
                        notEmpty: {
                            message: '返还积分不能为空'
                        },
                        regexp: {
                            regexp: /^[0-9\.]+$/,
                            message: '请填写正确的积分'
                        }
                    }
                }
            }
        });



        $.restPost('/commodityType/doQuery',{}).done(function (mess,data) {
            console.log(data);
            $form.find('.form-control[name="type"]').html(OptionsTpl(data.list))

            //判断是否是 编辑页面

            if(params){
                postUrl = '/commodity/doUpdate';
                $form.find('.form-control[name="title"]').val(params.title)
                $form.find('.form-control[name="currentCost"]').val(params.currentCost)
                $form.find('.form-control[name="type"]').val(params.type);
                $form.find('.form-control[name="unit"]').val(params.unit);
                $form.find('.form-control[name="backIndent"]').val(params.backIndent);

                uploaderCoverUrl($("#uploader-cover"),params.cover)
                editor.txt.html(params.desc)
                extra.txt.html(params.extra)
            }

            $("#submit").on("click",function () {
                var bootstrapValidator =  $form.data('bootstrapValidator');

                bootstrapValidator.validate();

                if(bootstrapValidator.isValid()){

                    if (CoverUrl == "") {
                        $.rootErrorDialog("产品图片不能为空", 2000)
                        return
                    }

                    var param = {
                        title:$form.find('.form-control[name="title"]').val(),
                        currentCost:$form.find('.form-control[name="currentCost"]').val(),
                        type:$form.find('.form-control[name="type"]').val(),
                        unit:$form.find('.form-control[name="unit"]').val(),
                        backIndent:$form.find('.form-control[name="backIndent"]').val(),
                        cover: CoverUrl,
                        desc:editor.txt.html(),
                        extra:extra.txt.text()
                    };


                    if(params){
                        param.id = params.id
                    }

                    $.restPost(postUrl, param).done(function (msg) {

                        $.rootSuccessDialog(msg, 1000,function () {
                            location.hash='/product/manage'
                        })

                    }).fail(function (msg) {

                        $.rootErrorDialog(msg, 1000)
                    })
                }

            })
        })




    };

    if (/edit/.test(location.hash)) {
        $.restPost("/commodity/doSelect", {
            id: $.getUrlLocation()
        }).done(function (mess, data) {
            init(data)
        })

    } else {
        init(false)

    }

})